package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.CollectionUtils;
import com.sinog.core.util.StrUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.IdGen;
import com.sinog.front.config.ThreadPoolConfig;
import com.sinog.front.feign.biz.DadcInfoService;
import com.sinog.front.service.user.UserFacade;
import com.sinog.front.util.FileUtil;
import com.sinog.front.util.FreemarkerTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @desc 安置帮教管理 - 档案管理 - DadcController
 * @author sunsf
 * @date 2020-02-28
 */
@Slf4j
@RestController
public class DadcController {

    @Autowired
    private ThreadPoolConfig threadPoolConfig;
    @Resource
    private UserFacade userService;
    @Resource
    private DadcInfoService dadcInfoService;
    private static final ThreadLocal<String> PNAME_LOCAL = new ThreadLocal<>();

    /**
     * 安置帮教管理 - 档案管理 - 导出
     * @param request request type:1:档案管理-导出 2:走访未走访 批量导出档案
     * @param response response
     */
    @RequestMapping("/azbj/azbjgl/dcda/exportData")
    public void exportData(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //1、获取参数并封装参数，定义时间
        String priNum = request.getParameter("priNum");
        String templateId = request.getParameter("templateId");
        String type = request.getParameter("type");
        String savePath = FileUtil.getFileTemplatePath();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = sdf.format(date);
        String format = sdf1.format(date);
        //1.1、将priNums拆分,可能导出多个文件
        String[] priNums = priNum.split(",");
        AtomicReference<ServletOutputStream> outputStream = new AtomicReference<>();
        //4.23、建档人 当前操作用户
        UserDTO user = userService.getAuthentication();
        //1.2 使用共享锁,将主线程阻塞
        CountDownLatch countDownLatch = new CountDownLatch(priNums.length);
        log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>档案导出共执行:{}次",priNums.length);
        for(String prinum : priNums) {
            threadPoolConfig.taskExecutor().execute(() -> {
                log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>档案导出线程池开始执行>>>>>>>>>>>>>>>>>>>>>>>>");
                log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>priNum:{}",prinum);
                log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>templateId:{}",templateId);
                try {
                    Map<String,Object> paramMap = new HashMap<>(16);
                    paramMap.put("priNum",prinum);
                    paramMap.put("templateId",templateId);
                    //2、定义文档导出父路径：/opt/azbj/uploadfile/fileTemplate/刑满释放解除矫正人员安置帮教档案_yyyy年MM月dd日_id/
                    String targetPath = savePath + "刑满释放解除矫正人员安置帮教档案_" + nowDate + "_" + IdGen.strid() + File.separator;
                    //3、导出所需信息封装
                    Map<String,Object> dataMap = new HashMap<>(16);
                    //4、查询数据开始
                    //4.1、获取基本信息
                    Map<String,Object> baseMap = dadcInfoService.getDachuBaseInfo(paramMap);
                    if(null != baseMap) {
                        dataMap.putAll(baseMap);
                    } else {
                        baseMap = new HashMap<>(16);
                    }
                    //4.2、定义返回文件名：刑满释放解除矫正人员安置帮教档案_${pname}_yyyy年MM月dd日.zip
                    String zipFileName = "刑满释放解除矫正人员安置帮教档案_" + baseMap.get("pname") + "_" + nowDate + ".zip";
                    // 将pname存入threadLocal一份
                    if(null != baseMap.get("pname")) {
                        PNAME_LOCAL.set((String)baseMap.get("pname"));
                    }
                    //4.3、获取照片
                    String path = dadcInfoService.getHeadPicByPrinum(priNum);
                    //4.4、定义上传路径：/opt/azbj/uploadfile/
                    String pathTempAppendix = FileUtil.getUploadFilePath();
                    //4.5、图片路径：/opt/azbj/uploadfile/bjsjyadmin/247229355393025.png，返回imageBase：图片base64编码
                    String imageBase = null;
                    try {
                        imageBase = StringUtils.isEmpty(path) ? "" : StrUtil.getImageBase(pathTempAppendix + path);
                    } catch(IOException e) {
                        log.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>档案导出发生异常:{}",e.getMessage());
                        e.printStackTrace();
                    }
                    dataMap.put("imageBase",imageBase);
                    //4.6、获取家庭信息
                    List<Map<String,Object>> jtcyList = dadcInfoService.getDachuJtcyInfo(paramMap);
                    if(null != jtcyList) {
                        jtcyList.removeAll(Collections.singleton(null));
                    }
                    dataMap.put("jtcyList",CollectionUtils.fillList(jtcyList,5));
                    //4.6、获取帮教人员信息
                    List<Map<String,Object>> bjryList = dadcInfoService.getDachuBjryInfo(paramMap);
                    bjryList = getListMaps(bjryList);
                    dataMap.put("bjryList",CollectionUtils.fillList(bjryList,5));
                    //4.7、根据帮教人员信息组装bjryReList
                    List<Map<String,Object>> bjryRxyList = new ArrayList<>(16);
                    bjryRxyList.addAll(bjryList);
                    bjryRxyList = CollectionUtils.fillList(bjryRxyList,3);
                    List<Map<String,Object>> bjryReList = new ArrayList<>(16);
                    for(Map<String,Object> map : bjryRxyList) {
                        Map<String,Object> reMap = new HashMap<>(16);
                        reMap.put("name",StrUtil.fillString((String)map.get("name"),3));
                        reMap.put("relation",StrUtil.fillString((String)map.get("relation"),2));
                        reMap.put("zzmm",StrUtil.fillString((String)map.get("zzmm"),5));
                        reMap.put("gzdw",StrUtil.fillString((String)map.get("gzdw"),6));
                        reMap.put("tel",StrUtil.fillString((String)map.get("tel"),6));
                        bjryReList.add(reMap);
                    }
                    dataMap.put("bjryReList",bjryReList);
                    //4.8、获取核查信息
                    List<Map<String,Object>> hcList = dadcInfoService.getDachuHcInfo(paramMap);
                    if(null != hcList) {
                        hcList.removeAll(Collections.singleton(null));
                    }
                    dataMap.put("hcList",CollectionUtils.fillList(hcList,3));
                    //4.9、获取安置信息
                    List<Map<String,Object>> azList = dadcInfoService.getDachuAzInfo(paramMap);
                    azList = getListMaps(azList);
                    List<Map<String,Object>> azListWithFj = getFjList(azList,priNum,targetPath,"安置附件","azid","azfj","25");
                    dataMap.put("azList",CollectionUtils.fillList(azListWithFj,3));
                    //4.10、获取救助信息
                    List<Map<String,Object>> jzList = dadcInfoService.getDachuJzInfo(paramMap);
                    jzList = getListMaps(jzList);
                    List<Map<String,Object>> jzListWithFj = getFjList(jzList,priNum,targetPath,"救助附件","jzid","jzfj","27_低保附件,28_特困附件,29_医疗附件,30_教育附件,31_住房附件,32_其他附件");
                    dataMap.put("jzList",CollectionUtils.fillList(jzListWithFj,3));
                    //4.10、获取帮扶信息
                    List<Map<String,Object>> bfList = dadcInfoService.getDachuBfInfo(paramMap);
                    bfList = getListMaps(bfList);
                    List<Map<String,Object>> bfListWithFj = getFjList(bfList,priNum,targetPath,"帮教附件","bfid","bffj","14_帮扶,15_党政家庭教育,16_走访,17_社会组织,18_志愿者,19_排查");
                    dataMap.put("bfList",CollectionUtils.fillList(bfListWithFj,3));
                    //4.11、获取衔接信息
                    Map<String,Object> xjMap = dadcInfoService.getDachuXjInfo(paramMap);
                    if(null != xjMap) {
                        String xmsftzs = getFjMap(priNum,targetPath,"刑满释放人员通知书","12");
                        dataMap.put("xmsftzs",xmsftzs);
                        dataMap.putAll(xjMap);
                    } else {
                        xjMap = new HashMap<>(16);
                    }
                    //4.12、获取出监评估信息
                    Map<String,Object> cjpgMap = dadcInfoService.getDachuCjpgInfo(paramMap);
                    if(null != cjpgMap) {
                        String cjpgfj = getFjMap(priNum,targetPath,"是否再犯罪评估附件","1");
                        dataMap.put("cjpgfj",cjpgfj);
                        dataMap.putAll(cjpgMap);
                    } else {
                        cjpgMap = new HashMap<>(16);
                    }
                    //4.13、衔接方式其他
                    dataMap.put("xjfsqt",StrUtil.fillString("",6));
                    //4.14、获取失联信息
                    List<Map<String,Object>> slList;
                    slList = dadcInfoService.getDachuSlInfo(paramMap);
                    for(Map<String,Object> map : slList) {
                        if(null != map && null != map.get("zhname")) {
                            map.put("slname",map.get("zhname"));
                        }
                    }
                    slList.removeAll(Collections.singleton(null));
                    dataMap.put("slList",CollectionUtils.fillList(slList,2));
                    //4.15、获取年度鉴定信息
                    List<Map<String,Object>> jdList = dadcInfoService.getDachuJdInfo(paramMap);
                    if(null != jdList) {
                        jdList.removeAll(Collections.singleton(null));
                    }
                    dataMap.put("jdList",CollectionUtils.fillList(jdList,11));
                    //4.17、获取机构信息
                    Map<String,Object> jgMap = dadcInfoService.getDachuJgInfo(paramMap);
                    if(null != jgMap) {
                        dataMap.putAll(jgMap);
                    } else {
                        jgMap = new HashMap<>(16);
                    }
                    //户籍地
                    String hjdz = (String)jgMap.get("hjdz");
                    String xxhjdz = (String)jgMap.get("xxhjdz");
                    //居住地
                    String xzdz = (String)jgMap.get("xzdz");
                    String xxxzdz = (String)jgMap.get("xxxzdz");
                    //户籍地址
                    dataMap.put("hujidixx",xxhjdz);
                    //居住地址
                    dataMap.put("juzhudixx",xxxzdz);
                    //封皮 - 建档单位
                    dataMap.put("jiandangdanwei",jgMap.get("sfsName"));
                    //4.18、封装各种信息
                    //建档时间
                    dataMap.put("jiandangshijian",sdf1.format(date));
                    //封皮下划线处理 14个汉字长度
                    //姓名
                    dataMap.put("cover_name",StrUtil.fillString((String)baseMap.get("pname"),14));
                    //帮教类别
                    dataMap.put("cover_bjtype",StrUtil.fillString((String)xjMap.get("bjtype"),14));
                    //户籍地址
                    dataMap.put("cover_hujidi",StrUtil.fillString(hjdz,14));
                    //居住地址
                    dataMap.put("cover_juzhudi",StrUtil.fillString(xzdz,14));
                    //联系电话
                    dataMap.put("cover_tel",StrUtil.fillString((String)baseMap.get("tel"),14));
                    //帮教期限
                    dataMap.put("cover_bjqx",StrUtil.fillString((String)baseMap.get("bjqx"),26));
                    //处理登记表
                    //处理省
                    dataMap.put("sfsPro",StrUtil.fillString((String)jgMap.get("nativePro"),4));
                    //处理市
                    dataMap.put("sfsCity",StrUtil.fillString((String)jgMap.get("nativeCity"),4));
                    //处理县
                    dataMap.put("sfsCou",StrUtil.fillString((String)jgMap.get("nativeCou"),4));
                    //处理乡镇
                    dataMap.put("ownsfsname",StrUtil.fillString((String)jgMap.get("nativevill"),6));
                    //帮教期限年月日
                    //开始-年
                    dataMap.put("bjqxsyear",StrUtil.fillString((String)baseMap.get("bjqxsyear"),2));
                    //开始-月
                    dataMap.put("bjqxsmonth",StrUtil.fillString((String)baseMap.get("bjqxsmonth"),2));
                    //开始-日
                    dataMap.put("bjqxsday",StrUtil.fillString((String)baseMap.get("bjqxsday"),2));
                    //结束-年
                    dataMap.put("bjqxeyear",StrUtil.fillString((String)baseMap.get("bjqxeyear"),2));
                    //结束-月
                    dataMap.put("bjqxemonth",StrUtil.fillString((String)baseMap.get("bjqxemonth"),2));
                    //结束-日
                    dataMap.put("bjqxeday",StrUtil.fillString((String)baseMap.get("bjqxeday"),2));
                    //通报和报告
                    //案件号
                    dataMap.put("tbajh",StrUtil.fillString("",2));
                    //公安派出所
                    dataMap.put("tbgapcs",StrUtil.fillString("",6));
                    //司法所
                    dataMap.put("tbsfsName",StrUtil.fillString((String)jgMap.get("nativevillName"),6));
                    //报告人民政府
                    dataMap.put("bgrmzf",StrUtil.fillString("",6));
                    //司法所
                    dataMap.put("tbownsfsname",StrUtil.fillString((String)jgMap.get("nativevill"),6));
                    //带下划线基本信息
                    //姓名
                    dataMap.put("tbname",StrUtil.fillString((String)baseMap.get("pname"),3));
                    //性别
                    dataMap.put("tbsex",StrUtil.fillString((String)baseMap.get("sex"),2));
                    //电话
                    dataMap.put("tbtel",StrUtil.fillString((String)baseMap.get("tel"),2));
                    //出生年月
                    dataMap.put("tbbirthmonth",StrUtil.fillString((String)baseMap.get("birthmonth"),5));
                    //民族
                    dataMap.put("tbnationality",StrUtil.fillString((String)baseMap.get("nationality"),3));
                    //罪名
                    dataMap.put("tbcritype",StrUtil.fillString((String)baseMap.get("critype"),6));
                    //刑期
                    dataMap.put("tbpritime",StrUtil.fillString((String)baseMap.get("pritime"),4));
                    //预释放日期
                    dataMap.put("tbreleasedate",StrUtil.fillString((String)baseMap.get("releasedate"),8));
                    //身份证号
                    dataMap.put("tbidnum",StrUtil.fillString((String)baseMap.get("idnum"),6));
                    //工作单位
                    dataMap.put("tbdanwei",StrUtil.fillString((String)baseMap.get("danwei"),6));
                    //住址
                    dataMap.put("tbjuzhudixx",StrUtil.fillString(xxxzdz,10));
                    //是否三无人员
                    dataMap.put("tbsfsanwu",StrUtil.fillString((String)cjpgMap.get("sfsanwu"),2));
                    //档案编号
                    dataMap.put("tbbjxysdabh",StrUtil.fillString("",6));
                    //重点帮教处理
                    String sfzhongdian = (String)Objects.requireNonNull(xjMap).get("sfzhongdian");
                    //4.19、犯罪信息,单独处理 dadcInfoService.getCretype((String)dataMap.get("critype"))
                    dataMap.put("critype",11);
                    //4.20、处理重点帮教信息
                    if(StringUtils.equals(sfzhongdian,Constants.NumberStr.NUM_STR_ZERO_ONE)) {
                        //案件号
                        dataMap.put("zdajh",StrUtil.fillString("",3));
                        //公安派出所
                        dataMap.put("zdgapcs",StrUtil.fillString("",3));
                        //司法所
                        dataMap.put("zdsfsName",StrUtil.fillString((String)jgMap.get("sfsName"),6));
                        //姓名
                        dataMap.put("zdname",StrUtil.fillString((String)baseMap.get("pname"),3));
                        //性别
                        dataMap.put("zdsex",StrUtil.fillString((String)baseMap.get("sex"),2));
                        //电话
                        dataMap.put("zdtel",StrUtil.fillString((String)baseMap.get("tel"),2));
                        //出生年月
                        dataMap.put("zdbirthmonth",StrUtil.fillString((String)baseMap.get("birthmonth"),5));
                        //民族
                        dataMap.put("zdnationality",StrUtil.fillString((String)baseMap.get("nationality"),3));
                        //罪名
                        dataMap.put("zdcritype",StrUtil.fillString((String)baseMap.get("critype"),6));
                        //刑期
                        dataMap.put("zdpritime",StrUtil.fillString((String)baseMap.get("pritime"),4));
                        //预释放日期
                        dataMap.put("zdreleasedate",StrUtil.fillString((String)baseMap.get("releasedate"),8));
                        //身份证号
                        dataMap.put("zdidnum",StrUtil.fillString((String)baseMap.get("idnum"),6));
                        //工作单位
                        dataMap.put("zddanwei",StrUtil.fillString((String)baseMap.get("danwei"),6));
                        //住址
                        dataMap.put("zdjuzhudixx",StrUtil.fillString(xxxzdz,10));
                    } else {
                        //案件号
                        dataMap.put("zdajh",StrUtil.fillString("",3));
                        //公安派出所
                        dataMap.put("zdgapcs",StrUtil.fillString("",3));
                        //司法所
                        dataMap.put("zdsfsName",StrUtil.fillString("",6));
                        //姓名
                        dataMap.put("zdname",StrUtil.fillString("",3));
                        //性别
                        dataMap.put("zdsex",StrUtil.fillString("",2));
                        //电话
                        dataMap.put("zdtel",StrUtil.fillString("",2));
                        //出生年月
                        dataMap.put("zdbirthmonth",StrUtil.fillString("",3));
                        //民族
                        dataMap.put("zdnationality",StrUtil.fillString("",2));
                        //罪名
                        dataMap.put("zdcritype",StrUtil.fillString("",6));
                        //刑期
                        dataMap.put("zdpritime",StrUtil.fillString("",4));
                        //预释放日期
                        dataMap.put("zdreleasedate",StrUtil.fillString("",8));
                        //身份证号
                        dataMap.put("zdidnum",StrUtil.fillString("",6));
                        //工作单位
                        dataMap.put("zddanwei",StrUtil.fillString("",6));
                        //住址
                        dataMap.put("zdjuzhudixx",StrUtil.fillString("",10));
                    }
                    //4.21、释放日期年月日
                    dataMap.put("relyear",StrUtil.fillString((String)baseMap.get("relyear"),2));
                    dataMap.put("relmonth",StrUtil.fillString((String)baseMap.get("relmonth"),2));
                    dataMap.put("relday",StrUtil.fillString((String)baseMap.get("relday"),2));
                    //4.22、衔接方式
                    dataMap.put("xjfscode",xjMap.get("sfzhongdian"));
                    dataMap.put("jiandangren",user.getDepartName());
                    //4.24、获取委托信息
                    Map<String,Object> weituoMap = dadcInfoService.getWeituoInfo(paramMap);
                    if(null != weituoMap) {
                        dataMap.putAll(weituoMap);
                    }
                    //4.25、处理导出文件
                    //"247399494221824"
                    Map<String,Object> tempMap = dadcInfoService.getTemplateName(templateId);
                    //获取模板名称
                    //"jcbjdamb.ftl";
                    String tName = null != tempMap ? (String)tempMap.get("tpath") : "";
                    //调用下载方法
                    String prefix = "刑满释放解除矫正人员安置帮教档案";
                    String id = IdGen.strid();
                    String sourceFilename = prefix + "_temp_" + id + ".doc";
                    String targetFilename = prefix + "_" + id + "_" + PNAME_LOCAL.get() + ".doc";
                    //填充模板，生成临时word文件，不带目录
                    FreemarkerTemplateUtil.isSaveWordFileSuccess(dataMap,savePath,targetFilename,tName);
                    if(Constants.NumberStr.NUM_STR_ONE.equals(type)) {
                        //读取临时word文件，生成目录，重新另存为文件
                        FileUtil.copyFile(savePath + targetFilename,targetPath,targetFilename);
                        response.setContentType("application/force-download");
                        response.setHeader("Content-Disposition","attachment;filename=" + new String(zipFileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1));
                        try {
                            outputStream.set(response.getOutputStream());
                            FileUtil.toZip(targetPath,outputStream.get(),true);
                        } catch(RuntimeException | IOException e) {
                            log.error("发生异常:",e);
                        }
                        // 删除指定文件夹
                        FileUtil.deleteDir(new File(targetPath));
                    } else if(Constants.NumberStr.NUM_STR_TWO.equals(type)) {
                        //读取临时word文件，生成目录，重新另存为文件
                        FileUtil.copyFile(savePath + targetFilename,savePath + format,targetFilename);
                        response.setContentType("application/force-download");
                        response.setHeader("Content-Disposition","attachment;filename=" + new String("档案导出.zip".getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1));
                        try {
                            outputStream.set(response.getOutputStream());
                        } catch(RuntimeException | IOException e) {
                            log.error("发生异常:",e);
                        }
                    }
                    //4.26、下载完成把两个文件都删了
                    File tempfile = new File(savePath + sourceFilename);
                    if(tempfile.exists()) {
                        tempfile.delete();
                    }
                    File targetfile = new File(savePath + targetFilename);
                    if(targetfile.exists()) {
                        targetfile.delete();
                    }
                } finally {
                    countDownLatch.countDown();
                    log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>档案导出成功记录priNum:{},剩余:{}次",prinum,countDownLatch.getCount());
                }
            });
        }
        countDownLatch.await();
        switch(type) {
            case Constants.NumberStr.NUM_STR_ONE:
            case Constants.NumberStr.NUM_STR_TWO:
                FileUtil.toZip(savePath + format,outputStream.get(),true);
                if(null != outputStream.get()) {
                    try {
                        outputStream.get().close();
                    } catch(IOException e) {
                        log.error("发生异常:",e);
                    }
                }
                // 删除指定目录文件
                File file = new File(savePath + format);
                if(file.exists()) {
                    FileUtil.deleteDir(file);
                }
            default:
        }
    }

    /**
     * 判断Map集合是否可以添加参数集合
     * @param jzList 查询到的数据集合
     * @return List<Map < String,Object>>
     */
    private List<Map<String,Object>> getListMaps(List<Map<String,Object>> jzList) {
        if(CollectionUtils.isNotEmpty(jzList)) {
            jzList.removeAll(Collections.singleton(null));
        } else {
            jzList = new ArrayList<>(16);
        }
        return jzList;
    }

    /**
     * 罪犯有多次记录，但每次记录只有一种适用
     * @param list list
     * @param priNum priNum
     * @param targetPath targetPath
     * @param prefix prefix
     * @param upId 附件id
     * @param key key
     * @param task 分类
     * @return List<Map < String,Object>>
     */
    private List<Map<String,Object>> getFjList(List<Map<String,Object>> list,String priNum,String targetPath,String prefix,String upId,String key,String task) {
        int size = list.size();
        for(int i = 0; i < size; i++) {
            //获取附件
            StringBuilder fj = new StringBuilder(16);
            Map<String,Object> fjMap = new HashMap<>(16);
            if(!StringUtils.equals(upId,"")) {
                String ufid = String.valueOf(list.get(i).get(upId));
                fjMap.put("ufid",ufid);
            }
            fjMap.put("priNum",priNum);
            String[] taskSp = task.split(",");
            for(String eachTask : taskSp) {
                String taskId;
                String eachName = "";
                if(eachTask.contains("_")) {
                    String[] each = eachTask.split("_");
                    taskId = each[0];
                    eachName = each[1];
                } else {
                    taskId = eachTask;
                }
                fjMap.put("taskid",taskId);
                List<Map<String,Object>> fjList = dadcInfoService.getAppendix(fjMap);
                String path = FileUtil.getUploadFilePath();
                int size1 = fjList.size();
                for(int j = 0; j < size1; j++) {
                    String srcPath = path + fjList.get(j).get("path");
                    File file = new File(srcPath);
                    String fileName = prefix + "_" + (i + 1) + (Objects.equals(eachName,"") ? "" : "_" + eachName) + "_" + (j + 1) + file.getName()
                                                                                                                                         .substring(file.getName()
                                                                                                                                                        .lastIndexOf('.'));
                    fj.append(fileName).append(";");
                    FileUtil.copyFile(srcPath,targetPath + "附件" + File.separator + prefix,fileName);
                }
            }
            list.get(i).put(key,"详情见:" + fj);
        }
        return list;
    }

    /**
     * 获取FjMap
     * @param priNum priNum
     * @param targetPath targetPath
     * @param prefix prefix
     * @param taskId taskId
     * @return String
     */
    private String getFjMap(String priNum,String targetPath,String prefix,String taskId) {
        Map<String,Object> fjMap = new HashMap<>(16);
        fjMap.put("taskid",taskId);
        fjMap.put("priNum",priNum);
        //获取附件
        List<Map<String,Object>> azfjList = dadcInfoService.getAppendix(fjMap);
        StringBuilder fjName = new StringBuilder("详情见:");
        String path = FileUtil.getUploadFilePath();
        int size = azfjList.size();
        for(int j = 0; j < size; j++) {
            String srcPath = path + azfjList.get(j).get("path");
            File file = new File(srcPath);
            if(file.exists()) {
                String fileName = prefix + "_" + (j + 1) + file.getName().substring(file.getName().lastIndexOf('.'));
                fjName.append(fileName).append(";");
                FileUtil.copyFile(srcPath,targetPath + "附件" + File.separator + prefix,fileName);
            }
        }
        return fjName.toString();
    }
}